package antlr4.myantlrtest.test.grammarsv4.protobuf3.jprotoc;

import antlr4.myantlrtest.gen.grammarsv4.protobuf3.Protobuf3Lexer;
import antlr4.myantlrtest.gen.grammarsv4.protobuf3.Protobuf3Parser;
import antlr4.myantlrtest.test.grammarsv4.protobuf3.jprotoc.gentlp.GenBean;
import antlr4.myantlrtest.test.grammarsv4.protobuf3.jprotoc.utill.Tools;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;


public class JProtoc {

    public static void main(String[] args) {
        try {
            if (args.length > 0) {

                String protoFileName = args[0];//proto文件
                String dir = null;
                String way = null;
                if (args.length > 1) {
                    dir = args[1];//目标目录
                }
                if (args.length > 2) {
                    way = args[2];//1.bean;  2.stuff;    3.lombok bean;  4.lombok stuff
                }

                String suffix = FileUtil.getSuffix(protoFileName);//文件名后缀

                if ("proto".equals(suffix)) {
                    Tools.beanName = Tools.firstUpper(FileUtil.getPrefix(protoFileName));
                    String content = FileUtil.readString(protoFileName, "UTF-8");
                    CodePointCharStream input = CharStreams.fromString(content);

                    Protobuf3Lexer lexer = new Protobuf3Lexer(input);

                    //只用token还是不够的,parse也是需要的
//                    List<? extends Token> allTokens = lexer.getAllTokens();
//                    for (Token token : allTokens) {
//                        System.out.println(token.getType() + token.getText());
//                    }

                    CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
                    Protobuf3Parser parser = new Protobuf3Parser(commonTokenStream);
                    ParseTreeWalker walker = new ParseTreeWalker();//新建一个标准的遍历器
                    walker.walk(new ProtocListener(), parser.proto());

                    System.out.println(JSON.toJSONString(Tools.protoInfo));

                    //确定生成目录
                    if (StrUtil.isNotBlank(dir)) {//目标目录
                        Tools.genDir = dir + "/" + Tools.protoInfo.getPackageName().replace(".", "/") + "/";
                    } else {//当前项目目录
                        Tools.genDir = Tools.projectDir + "/" + Tools.protoInfo.getPackageName().replace(".", "/") + "/";
                    }

                    if ("1".equals(way)) {
                        GenBean.gen(Tools.protoInfo);
                    } else if ("2".equals(way)) {

                    } else if ("3".equals(way)) {

                    } else if ("4".equals(way)) {

                    } else {
                        GenBean.gen(Tools.protoInfo);
                    }

                } else {
                    System.err.println("不是.proto文件");
                }
            } else {
                System.err.println("参数中需要提供.proto文件");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
